﻿using DotNetDAL.Entity;
using DotNetDAL.Entity.DbStruct;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace DotNetDAL.Connector
{
    /// <summary>
    /// 
    /// </summary>
    class DbActionParamHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table_name"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List<DbActionParamColumn> CreateParams(DbStructColumnCache cahceData, object data)
        {
            if(cahceData ==null || cahceData.columns == null || cahceData.columns.Count == 0)
            {
                throw new Exception(string.Format("表“{0}”中不存在字段", cahceData.table_name));
            }

            List<DbActionParamColumn> parList = new List<DbActionParamColumn>();
            if (data != null)
            {
                List<string> errorList = new List<string>();
                if (data is Dictionary<string, object>)
                {
                    Dictionary<string, object> _data = data as Dictionary<string, object>;
                    foreach (KeyValuePair<string,object> kvp in _data)
                    {
                        if (cahceData.IsExistColumn(kvp.Key))
                        {
                            DbStructColumnDTO dto = cahceData.columns.Find(col => col.column_name == kvp.Key);
                            parList.Add(new DbActionParamColumn()
                            {
                                name = kvp.Key,
                                value = kvp.Value,
                                isnull_able = dto.isnull_able,
                                is_primary_key = dto.is_primary_key
                            });
                        }
                        else
                        {
                            errorList.Add(kvp.Key);
                        }
                    }

                }
                else
                {
                    Type type = data.GetType();
                    List<PropertyInfo> plist = new List<PropertyInfo>(type.GetProperties());
                    List<FieldInfo> flist = new List<FieldInfo>(type.GetFields());

                    if (plist != null)
                    {
                        foreach (PropertyInfo pInfo in plist)
                        {
                            if (cahceData.IsExistColumn(pInfo.Name))
                            {
                                DbStructColumnDTO dto = cahceData.columns.Find(col => col.column_name == pInfo.Name);
                                parList.Add(new DbActionParamColumn()
                                {
                                    name = pInfo.Name,
                                    value = pInfo.GetValue(data),
                                    isnull_able = dto.isnull_able,
                                    is_primary_key = dto.is_primary_key
                                });
                            }
                            else
                            {
                                errorList.Add(pInfo.Name);
                            }
                        }
                    }

                    if (flist != null)
                    {
                        foreach (FieldInfo fInfo in flist)
                        {
                            if (cahceData.IsExistColumn(fInfo.Name))
                            {
                                DbStructColumnDTO dto = cahceData.columns.Find(col => col.column_name == fInfo.Name);
                                parList.Add(new DbActionParamColumn()
                                {
                                    name = fInfo.Name,
                                    value = fInfo.GetValue(data),
                                    isnull_able = dto.isnull_able,
                                    is_primary_key = dto.is_primary_key
                                });
                            }
                            else
                            {
                                errorList.Add(fInfo.Name);
                            }
                        }
                    }

                }

                if (errorList.Count > 0)
                {
                    throw new Exception(string.Format("表“{0}”中不存在“{1}”等字段"
                        , cahceData.table_name, string.Join(",", errorList)));
                }

            }
            

            if (parList.Count == 0)
            {
                throw new Exception("参数对象不能为空或非空无字段属性");
            }

            return parList;
        }


    }
}
